package com.testing.vip14login.service.Impl;

import com.testing.vip14login.mapper.UserMapper;
import com.testing.vip14login.model.Person;
import com.testing.vip14login.model.Result;
import com.testing.vip14login.model.User;
import com.testing.vip14login.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Classname UserServiceImpl
 * @Description 类型说明
 * @Date 2023/9/28 21:53
 * @Created by 特斯汀Roy
 */
@Service("userService")
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;

    @Override
    public Result login(String username, String password) {
        if (username != null && password != null) {
            if (username.length() > 2 && username.length() < 17
                    && password.length() > 2 && password.length() < 17) {
                User person = userMapper.selectOneUserByUserPwd(username, password);
                System.out.println("查到的person是" + person);
                //登录验证逻辑
                if (person != null) {
                    return Result.success("恭喜您，登录成功", "");
                } else {
                    return new Result(1001, "用户名或密码错误！", "");
                }
            } else {
                return new Result(1002, "用户名密码长度应是3-16位！", "");
            }
        } else {
            return new Result(1003, "用户名密码不能为空！", "");
        }
    }

    @Override
    public Result getUserInfo(String username) {
        User userInfo = userMapper.getUserInfo(username);
        if (userInfo == null) {
            return new Result(1050, "查询用户信息失败", username);
        }
        //用户名密码隐藏。
        userInfo.setPassword("**********");
        userInfo.setUsername("本用户");
        return Result.success("查询成功", userInfo);
    }
}
